package com.lebang.controller.sys;

import com.alibaba.fastjson.JSONObject;
import com.lebang.common.Login;
import com.lebang.domain.MemberCompensateRecord;
import com.lebang.entity.RequestHeaders;
import com.lebang.entity.sys.LbbUser;
import com.lebang.example.MemberCompensateRecordExample;
import com.lebang.model.MoneyCategory;
import com.lebang.model.Whether;
import com.lebang.model.enums.buss.TaskCategory;
import com.lebang.service.UserService;
import com.lebang.service.buss.MemberTradeService;
import com.lebang.service.sys.MemberCompensateRecordService;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;


/**
 * member_compensate_record
 *
 * @author xwj
 * @date 2020-08-06
 */
@Controller
@RequestMapping("/sys/membercompensaterecord")
public class MemberCompensateRecordController {
    @Autowired
    private MemberCompensateRecordService baseService;
    @Autowired
    private MemberTradeService memberTradeService;
    @Autowired
    private UserService userService;

    @Login
    @GetMapping("/list")
    public String list() {
        return "/memberreawardrecord/list";
    }

    @Login
    @GetMapping("/add")
    public String add(Long id, Model model) {
        MemberCompensateRecord vo = null;
        if (id != null) {
        } else {
            vo = new MemberCompensateRecord();
        }
        model.addAttribute("item", vo);
        return "/memberreawardrecord/edit";
    }


    @Login
    @PostMapping("/edit")
    @ResponseBody
    public JsonData edit(MemberCompensateRecord vo, HttpSession session) {
        String userid = (String) session.getAttribute("userid");
        LbbUser lbbUser = userService.get(userid);
        if(lbbUser.getUsername().equals("hwq")||lbbUser.getUsername().equals("fangjf")){

            if(vo.getType()==1){
                //补偿
                vo.setCreateTime(LocalDateTime.now());
                vo.setHasDeleted(0);

                memberTradeService.addMoney(vo.getMemberId(), vo.getMoney(),
                        "奖励补偿",
                        "奖励补偿" ,
                        "", new RequestHeaders(), "", new MoneyCategory(Whether.NO, TaskCategory.REAWARD).notMaster());

            }else if(vo.getType()==2){
                vo.setCreateTime(LocalDateTime.now());
                vo.setHasDeleted(0);

//            memberTradeService.walletClean(vo.getMemberId());
//            //mysql持久化
//            walletService.outLayMoney(vo.getMemberId(),vo.getMoney());
                return Callback.fail("fail", "扣除还未开发");
            }else{
                return Callback.fail("fail", "类型不存在");
            }

            baseService.insertModel(vo);

        }else{
            return Callback.fail("fail", "沒有权限");
        }


        return Callback.success("success", "修改成功");
    }

    @Login
    @PostMapping("/delete")
    @ResponseBody
    public JsonData delete(Long id) {
        baseService.deleteById(id);
        return Callback.success("success", "删除成功");
    }

    @Login
    @ApiOperation(value = "分页")
    @PostMapping("/list")
    @ResponseBody
    public JSONObject list(HttpServletRequest request, @RequestParam Integer page, @RequestParam Integer limit) {
        MemberCompensateRecordExample example = new MemberCompensateRecordExample();
        example.setOffset((page - 1) * limit.longValue());
        example.setLimit(limit);
        MemberCompensateRecordExample.Criteria criteria = example.createCriteria();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("data", baseService.selectByExample(example));
        jsonObject.put("count", baseService.countByExample(example));
        return jsonObject;
    }
}
